import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;

public class TileSheet
{
	public Image img;

	public Point tileSize;

	float x_scale, y_scale;

	TileSheet(Image img, Point tileSize, float max_x, float max_y, int map_h,
			int map_w)
	{
		this.img = img;
		this.tileSize = tileSize;
		x_scale = (max_x / map_w) / tileSize.x;
		y_scale = (max_y / map_h) / tileSize.y;
	}

	TileSheet(Image img, Point tileSize, int map_h,int map_w)
	{
		this.img = img;
		this.tileSize = tileSize;
		x_scale = 1;
		y_scale = 1;
	}
	
	public void draw(Graphics g, int x, int y, int index)
	{
		if (img.getWidth(null) > 0)
		{
			int rows = (int) (img.getWidth(null) / tileSize.x);
			int row = index / rows;
			int col = index % rows;
			int sx = col * tileSize.x, sy = row * tileSize.y; // source x and y

			g.drawImage(img, x, y, x + tileSize.x, y + tileSize.y, sx, sy, sx
					+ tileSize.x, sy + tileSize.y, null);
		}
		else
		{
			g.fillRect(x, y, tileSize.x, tileSize.y);
		}
	}
}